package Tracker.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import Tracker.server.model.Model;

public class Server {
	ServerSocket server;
	boolean listen;
	
	ArrayList<ClientHandler> clients;	
	Model model;
	
	public Server(Model m) {
		this.listen = false;
		this.model = m;
		this.clients = new ArrayList<ClientHandler>();
	}
	
	public void bind(int port) throws IOException {
		this.server = new ServerSocket(port);
	}
	
	public void listen() throws IOException {
		listen = true;
		while(listen) {
			Socket clientS = this.server.accept();
			
			try {
				ClientHandler clientH = new ClientHandler(this.model, clientS, this);
				clientH.run();
				this.clients.add(clientH);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
